﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace emlak
{
    public partial class frmDovizCevirici : Form
    {
        public frmDovizCevirici()
        {
            InitializeComponent();
        }
        double tl, dolar, euro;
        private void frmDovizCevirici_Load(object sender, EventArgs e)
        {

        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (txtTl.Text=="")
            {
                MessageBox.Show("Lütfen Bir Değer Giriniz:", "DÖVİZ ÇEVİRİCİ", MessageBoxButtons.OK,MessageBoxIcon.Error);
                
                txtTl.Focus();
                return;

   
            }


            DataSet myDoviz = new DataSet();
            myDoviz.ReadXml("http://www.tcmb.gov.tr/kurlar/today.xml");
            dataGridView1.DataSource = myDoviz.Tables[1];

            tl = Convert.ToDouble(txtTl.Text);
            string kur = dataGridView1.Rows[0].Cells["BanknoteBuying"].Value.ToString();
            kur = kur.Replace('.', ',');
            dolar = tl / (Convert.ToDouble(kur));
            txtDolar.Text = dolar.ToString("0.##");
            
            string kur1= dataGridView1.Rows[11].Cells["BanknoteBuying"].Value.ToString();
   
            kur1 = kur1.Replace('.', ',');

            euro = tl / (Convert.ToDouble(kur1));
            txtEuro.Text = euro.ToString("0.##");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtDolar.Text=="")
            {
                MessageBox.Show("Lütfen Bir Değer Giriniz:", "DÖVİZ ÇEVİRİCİ", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                
                txtDolar.Focus();
                return;
            }


            DataSet myDoviz = new DataSet();
            myDoviz.ReadXml("http://www.tcmb.gov.tr/kurlar/today.xml");
            dataGridView1.DataSource = myDoviz.Tables[1];

            dolar = Convert.ToDouble(txtDolar.Text);

            string kur = dataGridView1.Rows[0].Cells["BanknoteBuying"].Value.ToString();
            kur = kur.Replace('.', ',');
            tl = dolar * (Convert.ToDouble(kur));
            txtTl.Text = tl.ToString("0.##");

            string kur1 = dataGridView1.Rows[11].Cells["BanknoteBuying"].Value.ToString();
            string kur2 = dataGridView1.Rows[0].Cells["BanknoteBuying"].Value.ToString();
            kur1 = kur1.Replace('.', ',');
            kur2 = kur2.Replace('.', ',');


            euro = dolar / (Convert.ToDouble(kur1)/Convert.ToDouble(kur2));
            txtEuro.Text = euro.ToString("0.##");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (txtEuro.Text == "")
            {
                MessageBox.Show("Lütfen Bir Değer Giriniz:", "DÖVİZ ÇEVİRİCİ", MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtEuro.Focus();
                return;
            }
            DataSet myDoviz = new DataSet();
            myDoviz.ReadXml("http://www.tcmb.gov.tr/kurlar/today.xml");
            dataGridView1.DataSource = myDoviz.Tables[1];


            string kur1 = dataGridView1.Rows[11].Cells["BanknoteBuying"].Value.ToString();
            string kur2 = dataGridView1.Rows[0].Cells["BanknoteBuying"].Value.ToString();
            kur1 = kur1.Replace('.', ',');
            kur2 = kur2.Replace('.', ',');


              euro = Convert.ToDouble(txtEuro.Text);
              tl = euro * (Convert.ToDouble(kur1));

            txtTl.Text = tl.ToString("0.##");

            dolar = euro * (Convert.ToDouble(kur1)/Convert.ToDouble(kur2));
            txtDolar.Text = dolar.ToString("0.##");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtDolar.Text = "";
            txtEuro.Text = "";
            txtTl.Text = "";
            txtTl.Focus();

        }
        public void rakam(KeyPressEventArgs e)
        {

            if (char.IsDigit(e.KeyChar) || e.KeyChar == Convert.ToChar(Keys.Back))
            {
                e.Handled = false;
            }
            else
                e.Handled = true;
        }
        private void txtTl_KeyPress(object sender, KeyPressEventArgs e)
        {
            rakam(e);
        }

        private void txtDolar_KeyPress(object sender, KeyPressEventArgs e)
        {
            rakam(e);
        }

        private void txtEuro_KeyPress(object sender, KeyPressEventArgs e)
        {
            rakam(e);
        }

       

       
    
        }
   
              
                            
    }

